/*
 * @Author: szx
 * @Date: 2022-03-08 13:21:26
 * @LastEditTime: 2022-03-08 13:54:55
 * @Description:
 * @FilePath: \leetcode\2000-2099\2055\2055.js
 */
/**
 * @param {string} s
 * @param {number[][]} queries
 * @return {number[]}
 */
var platesBetweenCandles = function (s, queries) {
    const n = s.length;
    const preSum = new Array(n).fill(0);
    let sum = 0;
    for (let i = 0; i < n; i++) {
        if (s[i] == '*') {
            sum++;
        }
        preSum[i] = sum;
    }
    let l = -1,
        r = -1;
    const left = new Array(n).fill(-1);
    const right = new Array(n).fill(-1);
    for (let i = 0; i < n; i++) {
        if (s[i] == '|') l = i;
        if (s[n - i - 1] == '|') r = n - i - 1;
        left[i] = l;
        right[n - i - 1] = r;
    }
    const m = queries.length;
    const res = new Array(m).fill(0);
    for (let i = 0; i < m; i++) {
        const x = right[queries[i][0]],
            y = left[queries[i][1]];
        // console.log(x, y);
        if (x == -1 || y == -1 || x > y) continue;
        res[i] = preSum[y] - preSum[x];
    }
    // console.log(preSum, left, right);
    return res;
};
console.log(platesBetweenCandles('||*', [[2, 2]]));
// console.log(
//     platesBetweenCandles('***|**|*****|**||**|*', [
//         [0, 1],
//         [1, 8]
//     ])
// );
// console.log(
//     platesBetweenCandles('***|**|*****|**||**|*', [
//         [1, 17],
//         [4, 5],
//         [14, 17],
//         [5, 11],
//         [15, 16]
//     ])
// );
